import axios from "axios";
import baseUrl from "@/api/baseUrl";
// 根据环境配置获取域名

export default function (Vue) {
    Vue.prototype.baseHttpClient = {};
    var baseHttpClient = Vue.prototype.baseHttpClient;
    baseHttpClient.execute = function (current_node, url, methods, paramData, callBack) {
        var headers = {
            'Content-Type': 'application/json',
            'Authorization': ''
        }
        var tokenInfo = JSON.parse(sessionStorage.getItem('tokenInfo'));
        if (tokenInfo) {
            headers.Authorization = tokenInfo.access_token;
        }
        if (methods.toUpperCase() === 'GET') {
            axios({
                url: baseUrl + url + '?timestamp=' + new Date().getTime(),
                method: methods,
                params: paramData,
                headers: headers,
                timeout: 2000000,
                responseType: 'json',
            }).then(function (res) {
                switch (res.data.code) {
                    case '200': {
                        callBack(res.data);
                        break;
                    }
                    case '201': {
                        callBack(res.data);
                        break;
                    }
                    case '401': {
                        current_node.$router.push('/401');
                        break;
                    }
                    case '403': {
                        setTimeout(function () {
                            current_node.$router.push('/login');
                        }, 2000);
                        break;
                    }
                    default: {
                        console.log(res)
                        console.log(res.data.code)
                        current_node.$message.error(res.data.message);
                        break;
                    }
                }
            }).catch(function (res) {
                switch (res.code) {
                case '200': {
                        callBack(res.data);
                        break;
                    }
                case '201': {
                        callBack(res.data);
                        break;
                    }
                case '401': {
                        current_node.$router.push('/401');
                        break;
                    }
                case '403': {
                        setTimeout(function () {
                            current_node.$router.push('/login');
                        }, 2000);
                        break;
                    }
                default: {
                        current_node.$message.error(res.message);
                        break;
                    }
                }
            });
        } else {
            axios({
                url: baseUrl + url + '?timestamp=' + new Date().getTime(),
                method: methods,
                data: paramData,
                headers: headers,
                timeout: 2000000,
                responseType: 'json',
            }).then(function (res) {
                switch (res.data.code) {
                    case '200': {
                        callBack(res.data);
                        break;
                    }
                    case '201': {
                        callBack(res.data);
                        break;
                    }
                    case '401': {
                        current_node.$router.push('/401');
                        break;
                    }
                    case '403': {
                        setTimeout(function () {
                            current_node.$router.push('/login');
                        }, 2000);
                        break;
                    }
                    default: {
                        current_node.$message.error(res.data.message);
                        break;
                    }
                }
            }).catch(function (res) {
                console.log(res)
                switch (res.code) {
                    case '200': {
                        callBack(res.data);
                        break;
                    }
                    case '201': {
                        callBack(res.data);
                        break;
                    }
                    case '401': {
                        current_node.$router.push('/401');
                        break;
                    }
                    case '403': {
                        setTimeout(function () {
                            current_node.$router.push('/login');
                        }, 2000);
                        break;
                    }
                    default: {
                        current_node.$message.error(res.message);
                        break;
                    }
                }
            });
        }
    }
}